    //
//  RankingController.m
//  NBANews
//
//  Created by Liccon Chang on 11-1-10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RankingController.h"
#import "ColumnView.h"

@implementation RankingController

@synthesize segmentedControl;
@synthesize mainTable;
@synthesize LeagueDatas;
@synthesize PartitionDatas;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	self.title = @"排名";
	CGRect rect = self.view.frame;
	
	UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, 40)];
	UIImage* topBg = [UIImage imageNamed:@"nav_bg.png"];
    UIColor *topbgc = [[UIColor alloc] initWithPatternImage:topBg];
	topView.backgroundColor = topbgc;
	
	segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
	segmentedControl.frame = CGRectMake((rect.size.height - 220) / 2, 5, 220, 30);
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
	[segmentedControl insertSegmentWithTitle:@"联赛排名" atIndex:0 animated:FALSE];
	[segmentedControl insertSegmentWithTitle:@"分区排名" atIndex:1 animated:FALSE];
	segmentedControl.selectedSegmentIndex = 0;
	[segmentedControl addTarget:self action:@selector(SegmentedClick:) forControlEvents:UIControlEventValueChanged];
	[topView addSubview:segmentedControl];
	
	[self.view addSubview:topView];
	[topView release];
	
	
	rect.size.width = self.view.frame.size.height;
	rect.origin.x = 0;
	rect.origin.y = 40;
	rect.size.height = self.view.frame.size.width - TabBarHeight - NarBarHeight - StateBarHeight - 40;
	mainTable = [[UITableView alloc] initWithFrame:rect style:UITableViewStyleGrouped];
	mainTable.backgroundColor = [UIColor clearColor];
	mainTable.dataSource = self;
	mainTable.delegate = self;
	[self.view addSubview:mainTable];
	
	[self InsertAdvertisementsView];
	[self addNavExit];
}

- (void)viewDidAppear:(BOOL)animated{
	if (LeagueDatas == nil) {
		[self getRanking];
	}
}

- (void) SegmentedClick:(id)sender{
	if (segmentedControl.selectedSegmentIndex == 0) {
		
	}
	else {
		
	}
}

- (void) getRanking{
//	NSString* url = [NSString stringWithFormat:[Constant getAddress:ERanking],topGameInfo.sid];
//	network = [self submitURL:url withOperation:@"getRanking"];
}


- (void)completeFetch:(NetworkUtil *)obj{
	[super completeFetch:obj];
	//prase League Partition data
	//iRacesCard = [RegexParse RegexParseRacesCard:[network getResponse]];
	[mainTable reloadData];
	[network release];
	network =nil;
}

/////////////////////////////////////////////////////////////////
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 40.0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tView {
	if (segmentedControl.selectedSegmentIndex == 0) {
		return [LeagueDatas count];
	}
	else {
		return [PartitionDatas count];
	}
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.row %2 !=0) {
		cell.backgroundColor = [UIColor colorWithRed:234.0f/255.0f green:219.0f/255.0f blue:222.0f/255.0f alpha:1.0f];
	}
}

- (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section {
	TableData* tableData = nil;
	if (segmentedControl.selectedSegmentIndex == 0) {
		tableData = (TableData*)[LeagueDatas objectAtIndex:section];
	}
	else {
		tableData = (TableData*)[PartitionDatas objectAtIndex:section];
	}
	if (tableData) {
		return [tableData.RowDatas count];
	}
	return 0;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	TableData* tableData = nil;
	if (segmentedControl.selectedSegmentIndex == 0) {
		tableData = (TableData*)[LeagueDatas objectAtIndex:section];
	}
	else {
		tableData = (TableData*)[PartitionDatas objectAtIndex:section];
	}
	if (tableData) {
		return [tableData Title];
	}
	return @"";
}

-(UITableViewCell*)tableView:(UITableView *)tableView   cellForRowAtIndexPath:(NSIndexPath*)indexPath {
	
	static NSString *cellIndentifier = @"Cell";
	int row = [indexPath row];
	int section = [indexPath section];
	
	UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
	if(cell ==  nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier] autorelease];
	}
	
	TableData* tableData = nil;
	if (segmentedControl.selectedSegmentIndex == 0) {
		tableData = (TableData*)[LeagueDatas objectAtIndex:section];
	}
	else {
		tableData = (TableData*)[PartitionDatas objectAtIndex:section];
	}
	
	if (tableData) {
		RowData* rowData = [tableData.RowDatas objectAtIndex:row];
		UIView* cellView = [self CreateColumnView:rowData ColWidth:mainTable.frame.size.height];
		[cell.contentView addSubview:cellView];
		[cellView release];
	}
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	int row = [indexPath row];
	int section = [indexPath section];
	
	TableData* tableData = nil;
	if (segmentedControl.selectedSegmentIndex == 0) {
		tableData = (TableData*)[LeagueDatas objectAtIndex:section];
	}
	else {
		tableData = (TableData*)[PartitionDatas objectAtIndex:section];
	}
	if (tableData) {
		RowData* rowData = [tableData.RowDatas objectAtIndex:row];
		//linkurl
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[segmentedControl release];
	[mainTable release];
	[LeagueDatas release];
	[PartitionDatas release];
    [super dealloc];
}


@end
